home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / doc / indent.txt < prev    next >
Encoding:
Text File  |  2001-09-26  |  17.4 KB  |  470 lines

  1. *indent.txt*    For Vim version 6.0.  Last change: 2001 Sep 12
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. This file is about indenting C programs and other files.
  8.  
  9. 1. Indenting C programs        |C-indenting|
  10. 2. Indenting by expression    |indent-expression|
  11.  
  12. ==============================================================================
  13. 1. Indenting C programs                    *C-indenting*
  14.  
  15. The basics for C indenting are explained in section |30.2| of the user manual.
  16.  
  17. Vim has options for automatically indenting C program files.  These options
  18. affect only the indent and do not perform other formatting.  For comment
  19. formatting, see |format-comments|.
  20.  
  21. Note that this will not work when the |+smartindent| or |+cindent| features
  22. have been disabled at compile time.
  23.  
  24. There are in fact four methods available for indentation:
  25. 'autoindent'    uses the indent from the previous line.
  26. 'smartindent'    is like 'autoindent' but also recognizes some C syntax to
  27.         increase/reduce the indent where appropriate.
  28. 'cindent'    Works more cleverly than the other two and is configurable to
  29.         different indenting styles.
  30. 'indentexpr'    The most flexible of all: Evaluates an expression to compute
  31.         the indent of a line.  When non-empty this method overrides
  32.         the other ones.  See |indent-expression|.
  33. The rest of this section describes the 'cindent' option.
  34.  
  35. Note that 'cindent' indenting does not work for every code scenario.  Vim
  36. is not a C compiler: it does not recognize all syntax.
  37.  
  38. These four options control C program indenting:
  39. 'cindent'    Enables Vim to perform C program indenting automatically.
  40. 'cinkeys'    Specifies which keys trigger reindenting in insert mode.
  41. 'cinoptions'    Sets your preferred indent style.
  42. 'cinwords'    Defines keywords that start an extra indent in the next line.
  43.  
  44. If 'lisp' is not on and 'equalprg' is empty, the "=" operator indents using
  45. Vim's built-in algorithm rather than calling an external program.
  46.  
  47. See |autocommand| for how to set the 'cindent' option automatically for C code
  48. files and reset it for others.
  49.  
  50.                     *cinkeys-format* *indentkeys-format*
  51. The 'cinkeys' option is a string that controls Vim's indenting in response to
  52. typing certain characters or commands in certain contexts.  Note that this not
  53. only triggers C-indenting.  When 'indentexpr' is not empty 'indentkeys' is
  54. used instead.  The format of 'cinkeys' and 'indentkeys' is equal.
  55.  
  56. The default is "0{,0},0),:,0#,!^F,o,O,e" which specifies that indenting occurs
  57. as follows:
  58.  
  59.     "0{"    if you type '{' as the first character in a line
  60.     "0}"    if you type '}' as the first character in a line
  61.     "0)"    if you type ')' as the first character in a line
  62.     ":"    if you type ':' after a label or case statement
  63.     "0#"    if you type '#' as the first character in a line
  64.     "!^F"    if you type CTRL-F (which is not inserted)
  65.     "o"    if you type a <CR> anywhere or use the "o" command (not in
  66.         insert mode!)
  67.     "O"    if you use the "O" command (not in insert mode!)
  68.     "e"    if you type the second 'e' for an "else" at the start of a
  69.         line
  70.  
  71. Characters that can precede each key:
  72. !    When a '!' precedes the key, Vim will not insert the key but will
  73.     instead reindent the current line.  This allows you to define a
  74.     command key for reindenting the current line.  CTRL-F is the default
  75.     key for this.  Be careful if you define CTRL-I for this because CTRL-I
  76.     is the ASCII code for <Tab>.
  77. *    When a '*' precedes the key, Vim will reindent the line before
  78.     inserting the key.  If 'cinkeys' contains "*<Return>", Vim reindents
  79.     the current line before opening a new line.
  80. 0    When a zero precedes the key (but appears after '!' or '*') Vim will
  81.     reindent the line only if the key is the first character you type in
  82.     the line.  When used before "=" Vim will only reindent the line if
  83.     there is only white space before the word.
  84.  
  85. When neither '!' nor '*' precedes the key, Vim reindents the line after you
  86. type the key.  So ';' sets the indentation of a line which includes the ';'.
  87.  
  88. Special key names:
  89. <>    Angle brackets mean spelled-out names of keys.  For example: "<Up>",
  90.     "<Ins>" (see |key-notation|).
  91. ^    Letters preceded by a caret (^) are control characters.  For example:
  92.     "^F" is CTRL-F.
  93. o    Reindent a line when you use the "o" command or when Vim opens a new
  94.     line below the current one (e.g., when you type <Enter> in insert
  95.     mode).
  96. O    Reindent a line when you use the "O" command.
  97. e    Reindent a line that starts with "else" when you type the second 'e'.
  98. :    Reindent a line when a ':' is typed which is after a label or case
  99.     statement.  Don't reindent for a ":" in "class::method" for C++.  To
  100.     Reindent for any ":", use "<:>".
  101. =word    Reindent when typing the last character of "word".  "word" may
  102.     actually be part of another word.  Thus "=end" would cause reindenting
  103.     when typing the "d" in "endif" or "endwhile".  But not when typing
  104.     "bend".  Also reindent when completion produces a word that starts
  105.     with "word".  "0=word" reindents when there is only white space before
  106.     the word.
  107. =~word    Like =word, but ignore case.
  108.  
  109. If you really want to reindent when you type 'o', 'O', 'e', '0', '<', '>',
  110. '*', ':' or '!', use "<o>", "<O>", "<e>", "<0>", "<<>", "<>>", "<*>", "<:>" or
  111. "<!>", respectively, for those keys.
  112.  
  113. For an emacs-style indent mode where lines aren't indented every time you
  114. press Enter but only if you press Tab, I suggest:
  115.     :set cinkeys=0{,0},:,0#,!<Tab>,!^F
  116. You might also want to switch off 'autoindent' then.
  117.  
  118. Note: If you change the current line's indentation manually, Vim ignores the
  119. cindent settings for that line.  This prevents vim from reindenting after you
  120. have changed the indent by typing <BS>, <Tab>, or <Space> in the indent or
  121. used CTRL-T or CTRL-D.
  122.  
  123.                         *cinoptions-values*
  124. The 'cinoptions' option sets how Vim performs indentation.  In the list below,
  125. "N" represents a number of your choice (the number can be negative).  When
  126. there is an 's' after the number, Vim multiplies the number by 'shiftwidth':
  127. "1s" is 'shiftwidth', "2s" is two times 'shiftwidth', etc.  You can use a
  128. decimal point, too: "-0.5s" is minus half a 'shiftwidth'.  The examples below
  129. assume a 'shiftwidth' of 4.
  130.  
  131.     >N    Amount added for "normal" indent.  Used after a line that should
  132.           increase the indent (lines starting with "if", an opening brace,
  133.           etc.).  (default 'shiftwidth').
  134.  
  135.         cino=            cino=>2        cino=>2s >
  136.           if (cond)          if (cond)          if (cond)
  137.           {              {              {
  138.               foo;        foo;              foo;
  139.           }              }              }
  140. <
  141.     eN    Add N to the prevailing indent inside a set of braces if the
  142.           opening brace at the End of the line (more precise: is not the
  143.           first character in a line).  This is useful if you want a
  144.           different indent when the '{' is at the start of the line from
  145.           when '{' is at the end of the line.  (default 0).
  146.  
  147.         cino=            cino=e2        cino=e-2 >
  148.           if (cond) {          if (cond) {      if (cond) {
  149.               foo;            foo;        foo;
  150.           }              }              }
  151.           else              else          else
  152.           {              {              {
  153.               bar;          bar;              bar;
  154.           }              }              }
  155. <
  156.     nN    Add N to the prevailing indent for a statement after an "if",
  157.           "while", etc., if it is NOT inside a set of braces.  This is
  158.           useful if you want a different indent when there is no '{'
  159.           before the statement from when there is a '{' before it.
  160.           (default 0).
  161.  
  162.         cino=            cino=n2        cino=n-2 >
  163.           if (cond)          if (cond)          if (cond)
  164.               foo;            foo;        foo;
  165.           else              else          else
  166.           {              {              {
  167.               bar;          bar;              bar;
  168.           }              }              }
  169. <
  170.     fN    Place the first opening brace of a function or other block in
  171.           column N.  This applies only for an opening brace that is not
  172.           inside other braces and is at the start of the line.  What comes
  173.           after the brace is put relative to this brace.  (default 0).
  174.  
  175.         cino=            cino=f.5s        cino=f1s >
  176.           func()          func()          func()
  177.           {            {              {
  178.               int foo;            int foo;          int foo;
  179. <
  180.     {N    Place opening braces N characters from the prevailing indent.
  181.           This applies only for opening braces that are inside other
  182.           braces.  (default 0).
  183.  
  184.         cino=            cino={.5s        cino={1s >
  185.           if (cond)          if (cond)          if (cond)
  186.           {            {              {
  187.               foo;          foo;              foo;
  188. <
  189.     }N    Place closing braces N characters from the matching opening
  190.           brace.  (default 0).
  191.  
  192.         cino=            cino={2,}-0.5s    cino=}2 >
  193.           if (cond)          if (cond)          if (cond)
  194.           {            {          {
  195.               foo;          foo;              foo;
  196.           }              }                }
  197. <
  198.     ^N    Add N to the prevailing indent inside a set of braces if the
  199.           opening brace is in column 0.  This can specify a different
  200.           indent for whole of a function (some may like to set it to a
  201.           negative number).  (default 0).
  202.  
  203.         cino=            cino=^-2        cino=^-s >
  204.           func()          func()          func()
  205.           {              {              {
  206.               if (cond)        if (cond)      if (cond)
  207.               {            {          {
  208.               a = b;        a = b;          a = b;
  209.               }            }          }
  210.           }              }              }
  211. <
  212.     :N    Place case labels N characters from the indent of the switch().
  213.           (default 'shiftwidth').
  214.  
  215.         cino=            cino=:0 >
  216.           switch (x)          switch(x)
  217.           {              {
  218.               case 1:          case 1:
  219.               a = b;      a = b;
  220.               default:          default:
  221.           }              }
  222. <
  223.     =N    Place statements occurring after a case label N characters from
  224.           the indent of the label.  (default 'shiftwidth').
  225.  
  226.         cino=            cino==10 >
  227.            case 11:        case 11:  a = a + 1;
  228.                a = a + 1;          b = b + 1;
  229. <
  230.     lN    If N != 0 Vim will align with a case label instead of the
  231.           statement after it.
  232.  
  233.         cino=                cino=l1 >
  234.             switch (a) {          switch (a) {
  235.             case 1: {          case 1: {
  236.                     break;          break;
  237.                 }          }
  238. <
  239.     gN    Place C++ scope declarations N characters from the indent of the
  240.           block they are in.  (default 'shiftwidth').  A scope declaration
  241.           can be "public:", "protected:" or "private:".
  242.  
  243.         cino=            cino=g0 >
  244.           {              {
  245.               public:          public:
  246.               a = b;      a = b;
  247.               private:          private:
  248.           }              }
  249. <
  250.     hN    Place statements occurring after a C++ scope declaration N
  251.           characters from the indent of the label.  (default
  252.           'shiftwidth').
  253.  
  254.         cino=            cino=h10 >
  255.            public:        public:   a = a + 1;
  256.                a = a + 1;          b = b + 1;
  257. <
  258.     pN    Parameter declarations for K&R-style function declarations will
  259.           be indented N characters from the margin.  (default
  260.           'shiftwidth').
  261.  
  262.         cino=            cino=p0        cino=p2s >
  263.           func(a, b)          func(a, b)      func(a, b)
  264.               int a;          int a;              int a;
  265.               char b;          char b;              char b;
  266. <
  267.     tN    Indent a function return type declaration N characters from the
  268.           margin.  (default 'shiftwidth').
  269.  
  270.         cino=            cino=t0        cino=t7 >
  271.               int          int             int
  272.           func()          func()          func()
  273. <
  274.     +N    Indent a continuation line (a line that spills onto the next) N
  275.           additional characters.  (default 'shiftwidth').
  276.  
  277.         cino=              cino=+10 >
  278.           a = b + 9 *            a = b + 9 *
  279.               c;                  c;
  280. <
  281.     cN    Indent comment lines after the comment opener, when there is no
  282.           other text with which to align, N characters from the comment
  283.           opener.  (default 3).  See also |format-comments|.
  284.  
  285.         cino=              cino=c5 >
  286.           /*                /*
  287.              text.             text.
  288.            */                 */
  289. <
  290.     CN    When N is non-zero, indent comment lines by the amount specified
  291.           with the c flag above even if there is other text behind the
  292.           comment opener.  (default 0).
  293.  
  294.         cino=c0              cino=c0,C1 >
  295.           /********            /********
  296.             text.            text.
  297.           ********/            ********/
  298. <          (Example uses ":set comments& comments-=s1:/* comments^=s0:/*")
  299.  
  300.     (N    When in unclosed parentheses, indent N characters from the line
  301.           with the unclosed parentheses.  Add a 'shiftwidth' for every
  302.           unclosed parentheses.  When N is 0 or the unclosed parentheses
  303.           is the first non-white character in its line, line up with the
  304.           next non-white character after the unclosed parentheses.
  305.           (default 'shiftwidth' * 2).
  306.  
  307.         cino=              cino=(0 >
  308.           if (c1 && (c2 ||        if (c1 && (c2 ||
  309.                   c3))               c3))
  310.               foo;            foo;
  311.           if (c1 &&            if (c1 &&
  312.               (c2 || c3))        (c2 || c3))
  313.              {                   {
  314. <
  315.     uN    Same as (N, but for one level deeper.  (default 'shiftwidth').
  316.  
  317.         cino=              cino=u2 >
  318.           if (c123456789        if (c123456789
  319.               && (c22345            && (c22345
  320.                   || c3))              || c3))
  321. <
  322.     UN    When N is non-zero, do not ignore the indenting specified by
  323.           ( or u in case that the unclosed parentheses is the first
  324.           non-white character in its line.  (default 0).
  325.  
  326.         cino= or cino=(s      cino=(s,U1 >
  327.           c = c1 &&            c = c1 &&
  328.               (                (
  329.                c2 ||                c2 ||
  330.                c3                c3
  331.               ) && c4;            ) && c4;
  332. <
  333.     wN    When in unclosed parentheses and N is non-zero and either
  334.           using "(0" or "u0", respectively, or using "U0" and the unclosed
  335.           parentheses is the first non-white character in its line, line
  336.           up with the character immediately after the unclosed parentheses
  337.           rather than the first non-white character.  (default 0).
  338.  
  339.         cino=(0              cino=(0,w1 >
  340.           if (   c1            if (   c1
  341.              && (   c2        && (   c2
  342.                 || c3))            || c3))
  343.               foo;            foo;
  344. <
  345.     mN    When N is non-zero, line up a line starting with a closing
  346.           parentheses with the first character of the line with the
  347.           matching opening parentheses.  (default 0).
  348.  
  349.         cino=(s              cino=(s,m1 >
  350.           c = c1 && (            c = c1 && (
  351.               c2 ||            c2 ||
  352.               c3            c3
  353.               ) && c4;            ) && c4;
  354.           if (                if (
  355.               c1 && c2            c1 && c2
  356.              )                )
  357.               foo;            foo;
  358. <
  359.                     *java-cinoptions* *java-indenting*
  360.     jN    Indent java anonymous classes correctly.  The value 'N' is
  361.           currently unused but must be non-zero (e.g. 'j1'). 'j1' will
  362.           indent for example the following code snippet correctly: >
  363.  
  364.         object.add(new ChangeListener() {
  365.             public void stateChanged(ChangeEvent e) {
  366.             do_something();
  367.             }
  368.         });
  369. <
  370.     )N    Vim searches for unclosed parentheses at most N lines away.
  371.           This limits the time needed to search for parentheses.  (default
  372.           20 lines).
  373.  
  374.     *N    Vim searches for unclosed comments at most N lines away.  This
  375.           limits the time needed to search for the start of a comment.
  376.           (default 30 lines).
  377.  
  378.  
  379. The defaults, spelled out in full, are:
  380.     cinoptions=>s,e0,n0,f0,{0,}0,^0,:s,=s,l0,gs,hs,ps,ts,+s,c3,C0,(2s,us,
  381.            \U0,w0,m0,j0,)20,*30
  382.  
  383. Vim puts a line in column 1 if:
  384. - It starts with '#' (preprocessor directives), if 'cinkeys' contains '#'.
  385. - It starts with a label (a keyword followed by ':', other than "case" and
  386.   "default").
  387. - Any combination of indentations causes the line to have less than 0
  388.   indentation.
  389.  
  390. ==============================================================================
  391. 2. Indenting by expression                *indent-expression*
  392.  
  393. The basics for using flexible indenting are explained in section |30.3| of the
  394. user manual.
  395.  
  396. If you want to write your own indent file, it must set the 'indentexpr'
  397. option.  Setting the 'indentkeys' option is often useful.  See the
  398. $VIMRUNTIME/indent directory for examples.
  399.  
  400.  
  401. REMARKS ABOUT SPECIFIC INDENT FILES ~
  402.  
  403.  
  404. FORTRAN                            *fortran-indent*
  405.  
  406. Block if, select case, and where constructs are indented. Comments, labelled
  407. statements and continuation lines are indented if the Fortran is in free
  408. source form, whereas they are not indented if the Fortran is in fixed source
  409. form because of the left margin requirements. Hence manual indent corrections
  410. will be necessary for labelled statements and continuation lines when fixed
  411. source form is being used. For further discussion of the method used for the
  412. detection of source format see |fortran-syntax|.
  413.  
  414. Do loops ~
  415. All do loops are left unindented by default. Do loops can be unstructured in
  416. Fortran with (possibly multiple) loops ending on a labelled executable
  417. statement of almost arbitrary type. Correct indentation requires
  418. compiler-quality parsing. Old code with do loops ending on labelled statements
  419. of arbitrary type can be indented with elaborate programs such as Tidy
  420. (http://www.unb.ca/chem/ajit/f_tidy.htm). Structured do/continue loops are
  421. also left unindented because continue statements are also used for purposes
  422. other than ending a do loop. Programs such as Tidy can convert structured
  423. do/continue loops to the do/enddo form. Do loops of the do/enddo variety can
  424. be indented. If you use only structured loops of the do/enddo form, you should
  425. declare this by setting the fortran_do_enddo variable in your .vimrc as
  426. follows >
  427.  
  428.    let fortran_do_enddo=1
  429.  
  430. in which case do loops will be indented. If all your loops are of do/enddo
  431. type only in, say, .f90 files, then you should set a buffer flag with an
  432. autocommand such as >
  433.  
  434.   au! BufRead,BufNewFile *.f90 let b:fortran_do_enddo=1
  435.  
  436. to get do loops indented in .f90 files and left alone in Fortran files with
  437. other extensions such as .for.
  438.  
  439.  
  440. VERILOG                            *verilog-indent*
  441.  
  442. General block statements such as if, for, case, always, initial, function,
  443. specify and begin, etc., are indented.  The module block statements (first
  444. level blocks) are not indented by default.  you can turn on the indent with
  445. setting a variable in the .vimrc as follows: >
  446.  
  447.   let b:verilog_indent_modules = 1
  448.  
  449. then the module blocks will be indented.  To stop this, remove the variable: >
  450.  
  451.   :unlet b:verilog_indent_modules
  452.  
  453. To set the variable only for Verilog file.  The following statements can be
  454. used: >
  455.  
  456.   au BufReadPost * if exists("b:current_syntax")
  457.   au BufReadPost *   if b:current_syntax == "verilog"
  458.   au BufReadPost *     let b:verilog_indent_modules = 1
  459.   au BufReadPost *   endif
  460.   au BufReadPost * endif
  461.  
  462. Furthermore, setting the variable b:verilog_indent_width to change the
  463. indenting width (default is 'shiftwidth'): >
  464.  
  465.   let b:verilog_indent_width = 4
  466.   let b:verilog_indent_width = &sw * 2
  467. <
  468.  
  469.  vim:tw=78:ts=8:ft=help:norl:
  470.